<?php
class UserComment extends Zend_Db_Table_Abstract {
	const ID = "id";
	const TARGET_USER = "target_user";
	const MESSAGE = "message_id";
	
	protected $_primary = "id";
	protected $_name = "user_comment";
	
	protected $_referenceMap = array (
	'TargetUser' => array (
		'columns' => 'target_user', 
		'refTableClass' => 'User', 
		'refColumns' => 'id' 
	),
	'Message' => array (
		'columns' => 'message_id', 
		'refTableClass' => 'Message', 
		'refColumns' => 'id' 
	),
	);
	
	public function addNew($data) {
		$obj = $this->findByUnique($data[self::TARGET_USER], $data[self::MESSAGE]);
		if (!isset($obj)) {
			$this->insert($data);
		}
	}
	
	/**
	 * Find by unique constraint.
	 * 
	 * @param $targetUser
	 * @param $messageId
	 */
	public function findByUnique($targetUser, $messageId) {
		$where = $this->select()->where("target_user=?", $targetUser)->where("message_id=?", $messageId);
		return $this->fetchRow($where);
	}
}
?>